home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS08.ADF / C / SmallClock.c < prev    next >
C/C++ Source or Header  |  1986-04-02  |  6KB  |  186 lines

  1. /*
  2.  
  3. Newsgroups: net.micro.amiga
  4. Subject: A simple clock program
  5. Date-Received: 4 Mar 86 12:26:11 GMT
  6. Organization: Rutgers Univ., New Brunswick, N.J.
  7.  
  8. From: Mike (I'll be mellow when I'm dead) Meyer <mwm%ucbopal@BERKELEY.EDU>
  9.  
  10. I know, I know - everybody and their brother has a clock program. This
  11. one has the advantage that it lives completely in the screen title
  12. bar. It's just what I want from a clock, and I figure others might be
  13. interested in it, so here it is.
  14.  
  15. The only problem I still have is that sometimes, the drag bar will
  16. cover the time. If someone knows how to fix this (get refresh
  17. messages, maybe?), I'd like to hear about it.
  18.  
  19.  
  20.  * clock - a dumb, digital clock in the upper right-hand corner. Designed
  21.  *   to be small, not flexible!
  22.  *
  23.  * Copyright (c) 1986, Mike Meyer
  24.  * Revised and fixed up by Rick Wirch
  25.  *
  26.  * Permission is hereby granted to distribute this program for any purposes
  27.  * whatsoever, so long as this notice, including the above copyright, is
  28.  * included with the distribution. Unlike other people, I don't care if you
  29.  * make money off of this, so long as I get credit for having written it.
  30.  */
  31.  
  32. #include <exec/types.h>
  33. #include <devices/timer.h>
  34. #include <libraries/dos.h>
  35. #include <intuition/intuition.h>
  36.  
  37. #include <stdio.h>
  38. #undef DEBUG
  39.  
  40. #define INTUITION_REV   1
  41. /*
  42.  * Things to tweak:
  43.  *   WIN_WIDTH - the width of the window output screen. Should be 136.
  44.  *   WAIT_TIME - how long to wait between updates. Also the maximum
  45.  *      mis-time you can get.  Finally, it's the longest period of
  46.  *      time you have to put up with the clock broken. Measured in
  47.  *      seconds.
  48.  */
  49. #define WIN_WIDTH   90
  50. #define WAIT_TIME   15
  51.  
  52. static struct NewWindow   New_Window = {
  53.    (587 - WIN_WIDTH), 0,   /* on right edge, up on the top of the screen */
  54.    WIN_WIDTH, 10,          /* Just big enough for the time */
  55.    -1, -1,                 /* Default pens */
  56.    CLOSEWINDOW,            /* All we care about is closing */
  57.    WINDOWCLOSE             /* Borderless, fairly standard window */
  58.    | SMART_REFRESH | NOCAREREFRESH,
  59.    (struct Gagdet *) NULL,
  60.    (struct Image *) NULL,
  61.    "",                     /* Empty title */
  62.    (struct Screen *) NULL,
  63.    (struct BitMap *) NULL,
  64.    0, 0, 0, 0,             /* no change sizes, doesn't matter */
  65.    WBENCHSCREEN            /* Of course! */
  66.    };
  67.  
  68. static char   Date_Buffer[8];   /* Now you know where the time goes! */
  69.  
  70. static struct IntuiText   Date_Text = {
  71.    1, 0,            /* Use the standard pen colors */
  72.    JAM2,            /* Use both of them */
  73.    29, 1,            /* in the upper left-hand corner */
  74.    (struct TextAttr *) NULL,   /* Use default text */
  75.    Date_Buffer,         /* Buffer for time */
  76.    (struct IntuiText *) NULL   /* All of text */
  77.    };
  78.  
  79. struct IntuitionBase   *IntuitionBase;
  80.  
  81. /*
  82.  * Some things that need to be shared with done.
  83.  */
  84.  
  85. static struct Window      *Window = NULL;
  86. static struct timerequest Time_Req;
  87. static struct MsgPort     *Timer_Port = NULL;
  88. extern struct MsgPort     *CreatePort();
  89.  
  90. #ifdef   DEBUG
  91. static short         cli = FALSE;
  92. #endif
  93.  
  94. #ifdef   DEBUG
  95. main(argc, argv)
  96. int argc;
  97. char *argv;
  98. #else
  99. main()
  100. #endif
  101. {
  102.    register int      hours, minutes;
  103.    register char     *buffer = Date_Buffer;
  104.    struct DateStamp  now;
  105.    struct IntuiMessage   *Msg, *GetMsg();
  106.  
  107. #ifdef   DEBUG
  108.    if (argc)
  109.       cli = TRUE;
  110. #endif
  111.  
  112.    if ((IntuitionBase = (struct IntuitionBase *)
  113.        OpenLibrary("intuition.library", INTUITION_REV)) == NULL)
  114.       done(20, "Can't open Intuition library");
  115.  
  116.    if ((Timer_Port = CreatePort("Timer Port", 0)) == NULL)
  117.       done(20, "Can't create timer port");
  118.  
  119.    if (OpenDevice(TIMERNAME, UNIT_VBLANK, (char *) &Time_Req, 0) != NULL)
  120.       done(20, "Can't open timer device");
  121.  
  122.    if ((Window = (struct Window *) OpenWindow(&New_Window)) == NULL)
  123.       done(20, "Can't open window");
  124.  
  125.    Time_Req.tr_node.io_Message.mn_ReplyPort = Timer_Port;
  126.    Time_Req.tr_node.io_Command = TR_ADDREQUEST;
  127.    Time_Req.tr_node.io_Flags = 0;
  128.    Time_Req.tr_node.io_Error = 0;
  129.  
  130.    buffer[3] = ':';
  131.    buffer [0] = buffer[6] = ' ';
  132.  
  133.    for (;;) {
  134.       DateStamp(&now);
  135.       hours =  now.ds_Minute / 60;
  136.       minutes= now.ds_Minute % 60;
  137.       buffer[1] = hours / 10 + '0';
  138.       if (buffer[1] == '0')                        /* Blank out a possible */
  139.          buffer[1] = ' ';                          /* leading 0 */
  140.       buffer[2] = hours   % 10 + '0';
  141.       buffer[4] = minutes / 10 + '0';
  142.       buffer[5] = minutes % 10 + '0';
  143.  
  144.       WindowToFront( Window);                      /* bring window forward */
  145.       PrintIText(Window->RPort, &Date_Text, 0, 0);
  146.  
  147.       Time_Req.tr_time.tv_secs  = WAIT_TIME;
  148.       Time_Req.tr_time.tv_micro = 0;
  149.       SendIO((char *) &Time_Req.tr_node);
  150.       Wait (1 << Window    ->UserPort -> mp_SigBit |
  151.             1 << Timer_Port->mp_SigBit);
  152.  
  153.       while (Msg = GetMsg(Window -> UserPort)) {
  154.          if (Msg -> Class == CLOSEWINDOW) {
  155.             ReplyMsg(Msg);
  156.             done(0, "exit");
  157.             }
  158.          ReplyMsg(Msg);
  159.          }
  160.  
  161.       (void) GetMsg(Timer_Port);
  162.       }
  163.    /* NOTREACHED */
  164.    }
  165. /*
  166.  * done - just clean up that which is open, and then leave.
  167.  */
  168. done(how, why)
  169. int how;
  170. char *why;
  171.    {
  172.  
  173.    AbortIO((char *) &Time_Req . tr_node);
  174.    if (Window) CloseWindow(Window);
  175.    if (Time_Req.tr_node.io_Message.mn_ReplyPort)
  176.       CloseDevice(&Time_Req);
  177.    if (Timer_Port) DeletePort(Timer_Port);
  178. #ifdef   DEBUG
  179.    if (cli) printf("clock: %s\n", why);
  180. #endif
  181.    OpenWorkBench();                                /* As requested */
  182.    if (IntuitionBase) CloseLibrary(IntuitionBase);
  183.    exit( how);
  184.    }
  185.  
  186.